package com.ruoyi.oa.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.vo.OaMessageVo;
import com.ruoyi.oa.domain.bo.OaMessageBo;
import com.ruoyi.oa.service.IOaMessageService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息管理
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/message")
public class OaMessageController extends BaseController {

    private final IOaMessageService iOaMessageService;

    /**
     * 查询消息管理列表
     */
//    @SaCheckPermission("oa:message:list")
    @GetMapping("/list")
    public TableDataInfo<OaMessageVo> list(OaMessageBo bo, PageQuery pageQuery) {
        return iOaMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取个人消息提醒
     */
    @SaIgnore
    @GetMapping("/waitingMessage")
    public TableDataInfo<OaMessageVo> message() {
        return iOaMessageService.queryPage();
    }

    /**
     * 导出消息管理列表
     */
    @SaCheckPermission("oa:message:export")
    @Log(title = "消息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaMessageBo bo, HttpServletResponse response) {
        List<OaMessageVo> list = iOaMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "消息管理", OaMessageVo.class, response);
    }

    /**
     * 导出消息管理列表
     */
//    @SaCheckPermission("oa:message:export")
    @Log(title = "消息管理", businessType = BusinessType.EXPORT)
    @GetMapping("/queryList")
    public R<JSONObject> queryList(OaMessageBo bo) {
        JSONObject jsonObject = new JSONObject();
        List<OaMessageVo> list = iOaMessageService.queryList(bo);
        OaMessageVo lastOne = iOaMessageService.getLastOne(bo.getType());
        if (lastOne!=null){
            jsonObject.put("showData",lastOne);
        }
        if (CollectionUtil.isNotEmpty(list)){
            jsonObject.put("total",list.size());
        }else {
            jsonObject.put("total",0);
        }
        return R.ok(jsonObject);
    }

    /**
     * 获取消息管理详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("oa:message:query")
    @GetMapping("/{id}")
    public R<OaMessageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaMessageService.queryById(id));
    }

    /**
     * 新增消息管理
     */
//    @SaCheckPermission("oa:message:add")
    @Log(title = "消息管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaMessageBo bo) {
        return toAjax(iOaMessageService.insertByBo(bo));
    }

    /**
     * 修改消息管理
     */
//    @SaCheckPermission("oa:message:edit")
    @Log(title = "消息管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaMessageBo bo) {
        return toAjax(iOaMessageService.updateByBo(bo));
    }

    /**
     * 删除消息管理
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("oa:message:remove")
    @Log(title = "消息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaMessageService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @Log(title = "消息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/readMessage/{id}")
    public R<Void> readMessage(@PathVariable Long id) {
        return toAjax(iOaMessageService.readMessage(id) ? 1 : 0);
    }

    @Log(title = "消息管理", businessType = BusinessType.UPDATE)
    @GetMapping("/batchReadMessage")
    public R<Void> batchReadMessage(@RequestParam(value = "type", required = false) String type) {
        return toAjax(iOaMessageService.batchReadMessage(type) ? 1 : 0);
    }

    @GetMapping("/getAllHasAuth")
    public R<List<Map<String, Object>>> getAllHasAuth(@RequestParam(value = "deptId", required = false) String deptId,
                                                      @RequestParam(value = "nickName", required = false) String nickName) {
        List<Map<String, Object>> allHasAuth = iOaMessageService.getAllHasAuth(deptId,nickName);
        return R.ok(allHasAuth);
    }


    @GetMapping("/getAllNotAuth")
    public R<List<Map<String, Object>>> getAllNotAuth(@RequestParam(value = "deptId", required = false) String deptId,
                                                      @RequestParam(value = "nickName", required = false) String nickName) {
        List<Map<String, Object>> allNotAuth = iOaMessageService.getAllNotAuth(deptId,nickName);
        return R.ok(allNotAuth);
    }
}
